/********************************************************************/
/*            Header for XLCD module library functions              */
/*                                                                  */
/********************************************************************/
/* La base este código fue provista como código fuente de la librerías de
 * periféricos junto con XC16 y fue incorporada directamente en el proyecto porque
 * necesitaba modificaciones para adecuar al hardware del proyecto.
 */

#if defined(__dsPIC30F__)
#include <p30fxxxx.h>
#elif defined(__dsPIC33F__)
#include <p33Fxxxx.h>
#elif defined(__dsPIC33E__)
#include <p33Exxxx.h>
#elif defined(__PIC24H__)
#include <p24Hxxxx.h>
#elif defined(__PIC24E__)
#include <p24Exxxx.h>
#endif 


#ifndef _XLCD_H
#define _XLCD_H

/* #defines of the data pins and the corresponding tris pins */
/* The READ_PIN_X #defines must match the corresponding DATA_PIN_X defines */


#define DATA_PIN_7           LATEbits.LATE7
#define DATA_PIN_6           LATEbits.LATE6
#define DATA_PIN_5           LATEbits.LATE5
#define DATA_PIN_4           LATEbits.LATE4

#define READ_PIN_7		PORTEbits.RE7
#define READ_PIN_6		PORTEbits.RE6
#define READ_PIN_5		PORTEbits.RE5
#define READ_PIN_4		PORTEbits.RE4

#define TRIS_DATA_PIN_7       TRISEbits.TRISE7
#define TRIS_DATA_PIN_6       TRISEbits.TRISE6
#define TRIS_DATA_PIN_5       TRISEbits.TRISE5
#define TRIS_DATA_PIN_4       TRISEbits.TRISE4

#define AN_PIN_7              ANSELEbits.ANSE7
#define AN_PIN_6              ANSELEbits.ANSE6
#define AN_PIN_5              ANSELEbits.ANSE5
#define AN_PIN_4              ANSELEbits.ANSE4

/* #defines of the control pins and the corresponding tris pins for 33E, 24E */
#define E_PIN                 LATDbits.LATD4       /* PORT for E */
#define RW_PIN                LATDbits.LATD5       /* PORT for RW */
#define RS_PIN                LATGbits.LATG6      /* PORT for RS */

#define TRIS_E                TRISDbits.TRISD4    /* TRIS for E */
#define TRIS_RW               TRISDbits.TRISD5    /* TRIS for RW */
#define TRIS_RS               TRISGbits.TRISG6   /* TRIS for RS */

#define AN_RS                 ANSELGbits.ANSG6


/**********************************/
/* P-tec PCOG1602B LCD controller */
/**********************************/

/* Display ON/OFF Control defines */
#define DON                   	0x0F 	/* Display on     		*/
#define DOFF                  	0x0b 	/* Display off     		*/
#define CURSOR_ON           	0x0f  	/* Cursor on       		*/
#define CURSOR_OFF          	0x0d  	/* Cursor off      		*/
#define BLINK_ON              	0x0f  	/* Cursor Blink    		*/
#define BLINK_OFF             	0x0e  	/* Cursor No Blink 		*/

/* Some Command Type defines							*/
#define CLEAR_XLCD		0x01		/* Clears the LCD		*/
#define RETURN_CURSOR_HOME	0x02		/* Returns the cursor to the HOME position	*/


/* Entry Mode Options	*/
#define INCR_MODE		0x07		/* Increment mode		*/
#define DECR_MODE		0x05		/* Decrement Mode	*/
#define ENTIRE_SHIFT_OFF	0x06		/* Shift Entire Display 	*/
#define ENTIRE_SHIFT_ON         0x07		/* Dont Shift Entire Display	*/

/* Cursor or Display Shift defines */
#define SHIFT_CUR_LEFT        0x13  /* Cursor shifts to the left   */
#define SHIFT_CUR_RIGHT       0x17  /* Cursor shifts to the right  */
#define SHIFT_DISP_LEFT       0x1b  /* Display shifts to the left  */
#define SHIFT_DISP_RIGHT      0x1f  /* Display shifts to the right */

/* Function Set defines */
#define FOUR_BIT              0x2f  /* 4-bit Interface  */
#define EIGHT_BIT             0x3f  /* 8-bit Interface  */

/* defines for no of lines */
#define SINGLE_LINE           0x37  /* Single line display  */
#define TWO_LINE              0x3F  /* Two line display  */

/* defines for segment data transfer direction */
#define SEG1_50_SEG51_100     0x39  /* SEG1->SEG50 ,SEG51->SEG100 */
#define SEG1_50_SEG100_51     0x3d  /* SEG1->SEG50 ,SEG100->SEG51 */
#define SEG100_51_SEG50_1     0x3b  /* SEG100->SEG51,SEG50->SEG1 */
#define SEG100_51_SEG1_50     0x3f  /* SEG100->SEG51,SEG1->SEG50 */

/* defines for COM data transfer direction */

#define COM1_COM16            0x3e  /* Data transfer direction is COM1 to COM16 */

#define COM16_COM1            0x3f  /* Data transfer direction is COM16 to COM1 */

/* Function prototypes */

/* OpenXLCD
 * Configures I/O pins for external LCD
 */
void OpenXLCD(char);

/* SetCGRamAddr
 * Sets the character generator address
 */
void SetCGRamAddr(char);

/* SetDDRamAddr
 * Sets the display data address
 */
void SetDDRamAddr(unsigned char);

/* BusyXLCD
 * Returns the busy status of the LCD
 */
char BusyXLCD(void);

/* ReadAddrXLCD
 * Reads the current address
 */
char ReadAddrXLCD(void);

/* ReadDataXLCD
 * Reads a byte of data
 */
char ReadDataXLCD(void);

/* WriteCmdXLCD
 * Writes a command to the LCD
 */
void WriteCmdXLCD(char);

/* putsXLCD
 * Writes a string of characters to the LCD
 */
void PutsXLCD(char *);

/* WriteDataXLCD
 * Writes a data byte to the LCD
 */
void WriteDataXLCD(char);

#define putrsXLCD   PutsXLCD
#define getcXLCD    ReadDataXLCD
#define putcXLCD    WriteDataXLCD

#endif